<?php
/**
*	table
*/
namespace Rainrock\Framework\kernel\Html;



class HtmlTable extends HtmlLabel{
	
	protected $headerData = array();
	protected $data 	  = array();
	
	public static function gets()
	{
		$obj = new HtmlTable();
		$obj->addCss('rock-table');
		return $obj;
	}
	
	protected $label = 'table';
	
	public function addTr($tda)
	{
		$obj = new HtmlLabel('tr');
		$this->addCont($obj);
	}
	
	/**
	*	设置表头
	*/
	public function setHeader($farr)
	{
		$this->headerData = $farr;
		
		$thead = HtmlLabel::get('thead');
		
		$tr = HtmlLabel::get('tr');
		foreach($farr as $k=>$rs){
			$name = $rs['name'];
			$th = HtmlLabel::get('th')->addCss('rock-table-th')->addStyle('padding', '5px');
			$th->addCont($rs['fields']);
			if($name){
				$th->addCont('<br>');
				$th->addCont($name);
			}
			$tr->addCont($th);
		}
		$this->addCont($thead->addCont($tr));
	}
	
	/**
	*	设置数据
	*/
	public function setData($data)
	{
		$this->data = $data;
		foreach($data as $k=>$rs){
			$tr = HtmlLabel::get('tr')->addCss('rock-table-tr');
			foreach($this->headerData as $f=>$fs){
				$fid = $fs['fields'];
				$val = @$rs[$fid];
				if($val===null){
					$val='<font color=#ccccc>NULL</font>';
				}else{
					$val= htmlspecialchars($val);
				}
				$tr->addCont(
					HtmlLabel::get('td')->addCss('rock-table-td')
					->addCont($val)
				);
			}
			$this->addCont($tr);
		}
	}		
}